package com.personal.kousuan;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.personal.kousuan.db.CommonData;
import com.personal.kousuan.db.InitialData;
import com.personal.kousuan.db.MyDbHelper;
import com.personal.kousuan.db.MyDbInfo;

public class MainActivity extends Activity implements OnClickListener{

	private Button btn_setting;
	private Button btn_exercise;
	public static MyDbHelper db = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialDbData();
		CommonData.getInstance().load(this);
		btn_setting = (Button)findViewById(R.id.nav_setting_btn);
		btn_exercise = (Button)findViewById(R.id.nav_start_exercise_btn);
		btn_setting.setOnClickListener(this);
		btn_setting.setOnClickListener(this);
		
	}
	
	private void initialDbData(){
		db = MyDbHelper.getInstance(getApplicationContext());
		db.open();
		
		Cursor cursor = db.select(MyDbInfo.getTableNames()[0], MyDbInfo.getFieldNames()[0], null, null, null, null, null);
		if(cursor.moveToNext()){
			cursor.close();
			return;
		}
		
		String[] semester = InitialData.INITIAL_SEMESTER;
		int index = 0;
		for(int i = 0;i<semester.length;i++){
			db.insert(MyDbInfo.getTableNames()[0],MyDbInfo.getFieldNames()[0],new String[]{String.valueOf(i),semester[i]});
			
			String[][] category = InitialData.INITIAL_CATEGORY[i];
			for(int j = 0;j<category.length;j++){
				db.insert(MyDbInfo.getTableNames()[1], MyDbInfo.getFieldNames()[1], new String[]{String.valueOf(index),String.valueOf(i),category[j][0],category[j][1]});
				index++;
				}
			}
		}
		
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.nav_setting_btn:
			Intent intent = new Intent(this,SettingActivity.class);
			startActivity(intent);
			break;
		case R.id.nav_start_exercise_btn:
			
			break;
			
		}
		
	}
}
